//
//  enumUtil.swift
//  万才信息App
//
//  Created by 陌上互联  on 16/7/15.
//  Copyright © 2016年 万才信息. All rights reserved.
//

import Foundation
import UIKit
import Haneke

import Foundation
import UIKit
import Haneke

//常量数据服务
class enumUtil {
    
    //    行业信息
    static var INDUSRTY_ENUM=[(Int,String)]()
    //    职位大类
    static var JOB_FUNC_PARENT=[(Int,String)]()
    //    大类中的小类
    static var JOB_CHILDS=[Int:[(Int,String)]]()
    //    省
    static var PROVINCE=[(Int,String)]()
    //    省的对应的城市
    static var PROVINCE_CITYS=[Int:[(Int,String)]]()
    //    简历信息－>性别
    static var RESUME_SEX=[(Int,String)]()
    //    简历信息－>婚姻状况
    static var RESUME_MARRIAGE=[(Int,String)]()
    //    简历信息－>学历
    static var RESUME_DEGREE=[(Int,String)]()
    //    简历信息－>工作年限
    static var RESUME_WORKYEAR=[(Int,String)]()
    //    简历信息－>工作性质
    static var RESUME_WORKNATURE=[(Int,String)]()
    //    简历信息－>薪资范围
    static var RESUME_SALARY_RANGE=[(Int,String)]()
    //    简历信息－>政治面貌
    static var RESUME_POLICTICAL_STATUS=[(Int,String)]()
    //    简历信息－>外语语种
    static var RESUME_LANGUAGE_TYPE=[(Int,String)]()
    //    简历信息－>外语程度（熟练，一般）
    static var RESUME_LANGUAGE_MASTER=[(Int,String)]()
    //    公司性质->公司性质（国有，私有）
    static var COMPANY_NATURE=[(Int,String)]()
    //    公司性质－>公司规模
    static var COMPANY_SCALE=[(Int,String)]()
    //    工作性质
    static var JOB_NATURE=[(Int,String)]()
    
    
    // 工具函数
    static func getStringByKey(key:Int,dict:[(Int,String)])->String{
        for element in dict{
            if(element.0 == key){
                return element.1
            }
        }
        return ""
    }
    
    static func getKeyByString(string:String,dict:[(Int,String)])->Int{
        for element in dict{
            if(element.1 == string){
                return element.0
            }
        }
        return 0
    }
    
    static func getOrderedStringArray(dict:[(Int,String)])->[String]{
        var strings=[String]()
        for element in dict{
            strings.append(element.1)
        }
        return strings
    }
    
    static func getParentCodeByChildCode(cityCode:Int)->Int{
//        获取前两位
        return 100*(cityCode/Int(100))
    }
    //    获取所有的enum信息并存取到数据库，注意一定要在函数入口处调用!
    static func getAllEnum(){
        getIndustryEnum()
        getJobEnum()
        getProvinceAndCity()
        getCompany()
        getResume()
//        print("测试是否存在：")
//        print(self.RESUME_DEGREE)
    }
    
    //    获取职位大类和小类信息
    private static func getJobEnum(){
        basicInfo.getJobType({ (data) in
            
            let rows=data["rows"].array
            print("获取职位大类信息成功")
            
            for row in rows!{
                let key=Int(row["FuncId"].string!)
                let childKey=Int(row["FunCode"].string!)
                let value=row["FunName"].string
                JOB_FUNC_PARENT.append((childKey!,value!))
                //                查找一下小类
                getJobChildById(key!,FunCode: childKey!)
            }
        }) { (error) in
            print(error)
        }
    }
    //    获取职位小类byID
    private static func getJobChildById(parentId:Int,FunCode:Int){
        basicInfo.getJobByParent(parentId, successCallback: { (data) in
            
            let rows=data["rows"].array
            print("获取职位小类成功 by ID:")
            print(String(parentId))
            
            var JOBS=[(Int,String)]()
            
            for row in rows!{
                let key=Int(row["FunCode"].string!)
                let value=row["FunName"].string
                
                JOBS.append((key!,value!))
            }
            
            JOB_CHILDS[FunCode]=JOBS
            
            if JOBS.isEmpty{
                PROVINCE_CITYS[FunCode]=[(FunCode,"")]
            }

            
        }) { (error) in
            print(error)
        }
    }
    
    //    获取简历
    private static func getResume(){
        getResumeByEnumId(1)
        getResumeByEnumId(2)
        getResumeByEnumId(3)
        getResumeByEnumId(4)
        getResumeByEnumId(5)
        getResumeByEnumId(6)
        getResumeByEnumId(8)
        getResumeByEnumId(9)
        getResumeByEnumId(10)
    }
    
    //    获取简历信息,第二个参数传递引用
    private static func getResumeByEnumId(enumId:Int){
        basicInfo.getResumeEnum(enumId, successCallback: { (data) in
            
            let rows=data["rows"].array
            print("获取简历信息成功 by EnumID:")
            print(enumId)
            var dict = [(Int,String)]()
            
            for row in rows!{
                let key=Int(row["EnumId"].string!)
                let value=row["EnumName"].string
                dict.append((key!,value!))
            }
            
            switch enumId{
            case 1:
                self.RESUME_SEX=dict
                break
            case 2:
                self.RESUME_MARRIAGE=dict
                break
            case 3:
                self.RESUME_DEGREE=dict
                break
            case 4:
                self.RESUME_WORKYEAR=dict
                break
            case 5:
                self.RESUME_WORKNATURE=dict
                break
            case 6:
                self.RESUME_SALARY_RANGE=dict
                break
            case 8:
                self.RESUME_POLICTICAL_STATUS=dict
                break
            case 9:
                self.RESUME_LANGUAGE_TYPE=dict
                break
            case 10:
                self.RESUME_LANGUAGE_MASTER=dict
                break
            default:
                break
            }
        }) { (error) in
            print(error)
        }
    }
    
    //    获取省市信息
    private static func getProvinceAndCity(){
        basicInfo.getProvince({ (data) in
            
            let rows=data["rows"].array
            print("获取省份成功")
            
            for row in rows!{
                let key=Int(row["CityCode"].string!)
                let value=row["CityName"].string
                PROVINCE.append((key!,value!))
                let ID=Int(row["ID"].string!)
                getCityById(ID!,cityCode: key!,provinceName: value!)
            }
            
        }) { (error) in
            print(error)
        }
    }
    //    根据省份获取城市信息
    private static func getCityById(parentID:Int,cityCode:Int,provinceName:String){
        basicInfo.getCity(String(parentID), successCallback: { (data) in
            let rows=data["rows"].array
            print("获取城市成功 by ID:")
            print(parentID)
            
            var IDS=[(Int,String)]()
            
            for row in rows!{
                let key=Int(row["CityCode"].string!)
                let value=row["CityName"].string
                IDS.append((key!,value!))
            }
            
            PROVINCE_CITYS[cityCode]=IDS
            
            if IDS.isEmpty{
                PROVINCE_CITYS[cityCode]=[(cityCode,provinceName)]
            }
            
            }, failCallback: { (error) in
                print(error)
        })
        
    }
    //    获取公司信息
    private static func getCompany(){
        //        获取公司性质
        basicInfo.getCompanyNature({ (data) in
            let rows=data["rows"].array
            print("获取公司性质成功")
            
            for row in rows!{
                let key=Int(row["EnumId"].string!)
                let value=row["EnumName"].string

                COMPANY_NATURE.append((key!,value!))
            }
            
        }) { (error) in
            print(error)
        }
        //        获取公司规模
        basicInfo.getCompanyScale({ (data) in
            let rows=data["rows"].array
            print("获取公司规模成功")
            
            for row in rows!{
                let key=Int(row["EnumId"].string!)
                let value=row["EnumName"].string
                COMPANY_SCALE.append((key!,value!))
            }
        }) { (error) in
            print(error)
        }
    }
    
    //    获取行业信息数据接口
    private static func getIndustryEnum(){
        basicInfo.getIndustry({ (data) in
            let rows=data["rows"].array
            print("获取行业信息成功")
            for row in rows!{
                let key=Int(row["IndCode"].string!)
                let value=row["IndName"].string
                INDUSRTY_ENUM.append((key!,value!))
            }
        }) { (error) in
            print("获取行业信息失败")
        }
    }
    
}

